package com.gec.teams.wechat.task;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.gec.teams.wechat.config.SystemConstants;
import com.gec.teams.wechat.mapper.SysConfigMapper;
import com.gec.teams.wechat.pojo.SysConfig;
import com.gec.teams.wechat.util.SysDateTime;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.List;

/**
 * 功能：
 * 作者：YumTea
 * 日期：2024/6/21 14:32
 */
@Component
@Slf4j
public class SysTask {
    @Autowired
    private SystemConstants systemConstants;
    @Autowired
    private SysConfigMapper sysConfigMapper;
    @Autowired
    private SysDateTime sysDateTime;

    //每天陵城3点  都查一遍数据库看看有没有开始考勤吧
    @Scheduled(cron = "0 0 3 * * ?")
//    @Scheduled(cron = "0/10 * * * * ? ")
    public void executeTask(){
        List<SysConfig> sysConfigs = sysConfigMapper.getAllSysConfig();
        sysConfigs.forEach(one->{
            String paramKey = one.getParamKey();
            //下划线转换大小写
            paramKey = StrUtil.toCamelCase(paramKey);
            String today = DateUtil.today();
            String value = today+" "+ one.getParamValue();
            try{
                //利用反射，来把sysConfig里面的值，反射到SystemConstants的属性名中
                Field filed = systemConstants.getClass().getDeclaredField(paramKey);//获取config中的属性名
                filed.set(systemConstants,value);//把对应属性名里对应的值放进去
            }catch (Exception e){
                log.error("配置初始化执行异常");
            }
        });
        System.out.println(systemConstants);
        sysDateTime.init();
    }
    }

